﻿#include "GUIDeleteDialog.h"
#include "ui_GUIDeleteDialog.h"

#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/PickedDataProvider.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEFace.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSensors.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"

namespace GUI
{
    GUIDeleteDialog::GUIDeleteDialog():
        GUIDialogBase(FITKAPP->getGlobalData()->getMainWindow())
    {
        _ui = new Ui::GUIDeleteDialog();
        _ui->setupUi(this);
        _pickWidget = new GUIPickWidget();
        _ui->verticalLayout->addWidget(_pickWidget);
        setWindowTitle(tr("Delete Dialog"));
    }

    GUIDeleteDialog::~GUIDeleteDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }

        if (_pickWidget) {
            delete _pickWidget;
            _pickWidget = nullptr;
        }
    }

    void GUIDeleteDialog::on_pushButton_Delete_clicked()
    {
        if (_pickWidget == nullptr)return;
        QList<int> idList = _pickWidget->getPickValue();
        //清空拾取
        _pickWidget->resetPickToNone();
        //删除拾取
        deleteEvent(_pickWidget->getCurrentPickType(), idList);
        //重置当前拾取状态
        _pickWidget->resetPickToCurrentSelect();
    }

    void GUIDeleteDialog::on_pushButton_Close_clicked()
    {
        this->close();
    }

    void GUIDeleteDialog::deleteEvent(GUIDialogPickType type, QList<int> idList)
    {
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (_pickWidget == nullptr && operGraph == nullptr)return;

        switch (type) {
        case GUI::GUIDialogPickType::Geometry: {
            break;
        }
        case GUI::GUIDialogPickType::SubSysteam: {
            //SEA子系统
            if (_SEAModel && _SEAModel->getSEASubSysManager()) {
                for (int i : idList) {
                    _SEAModel->getSEASubSysManager()->removeDataByID(i);
                }
            }
            //FE子系统
            if (_FEModel && _FEModel->getFESubSysManager()) {
                for (int i : idList) {
                    _FEModel->getFESubSysManager()->removeDataByID(i);
                }
            }
            //FE面
            if (_FEModel && _FEModel->getFEFaceManager()) {
                for (int i : idList) {
                    _FEModel->getFEFaceManager()->removeDataByID(i);
                }
            }
            //传感器
            if (_FEModel && _FEModel->getSensorManager()) {
                for (int i : idList) {
                    _FEModel->getSensorManager()->removeDataByID(i);
                }
            }
            operGraph->updateSEASubSystems();
            operGraph->updateFESubSystems();
            operGraph->updateFEFaces();
            operGraph->updateSensors();
            break;
        }
        case GUI::GUIDialogPickType::Load: {
            if (_physics && _physics->getFITKAcousticsLoadManager()) {
                for (int i : idList) {
                    _physics->getFITKAcousticsLoadManager()->removeDataByID(i);
                }
            }
            operGraph->updateLoads(true);
            break;
        }
        case GUI::GUIDialogPickType::Junction: {
            if (_DiscModel && _DiscModel->getJunctionManager()) {
                for (int i : idList) {
                    _DiscModel->getJunctionManager()->removeDataByID(i);
                }
            }
            operGraph->updateJunctions();
            break;
        }
        }

        //for (int id : idList) {
        //    operGraph->updateGraph(id,true);
        //}
    }
}